package org.zwt;

import org.zwt.common.formatter.impl.SimpleRequestFormatter;
import org.zwt.common.formatter.impl.SimpleResponseFormatter;
import org.zwt.request.impl.SimpleRequestHandler;
import org.zwt.response.impl.SimpleResponseHandler;
import org.zwt.server.HttpServer;
import org.zwt.view.impl.SimpleViewResolver;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ServerStarter {
    public static void main(String[] args) {
        // 数据库连接设置
        String url = "jdbc:mysql://localhost:3306/user_db";
        String user = "root";
        String password = "123456";

        try (Connection connection = DriverManager.getConnection(url, user, password)) {
            if (connection != null) {
                System.out.println("数据库连接成功!");
            }
        } catch (SQLException e) {
            System.err.println("数据库连接失败: " + e.getMessage());
            e.printStackTrace();
            return; // 如果数据库连接失败，则退出
        }

        try {
            HttpServer server = new HttpServer(3000);
            server.setRequestHandler(new SimpleRequestHandler(new SimpleRequestFormatter()))
                    .setViewResolver(new SimpleViewResolver("page/", ".html"))
                    .setResponseHandler(new SimpleResponseHandler(new SimpleResponseFormatter()));

            System.out.println("服务器启动中...");
            server.start();
        } catch (Exception e) {
            System.err.println("服务器启动失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
}